import 'package:flutter/material.dart';
import 'package:flutterhello/api/api.dart';
import 'package:flutterhello/pages/new_team/team_act.dart';
import 'package:flutterhello/pages/new_team/team_users.dart';
import "package:extended_nested_scroll_view/extended_nested_scroll_view.dart"
    as extended;
import 'package:flutterhello/util/host_util.dart';
import 'package:get/get.dart';

class Team extends StatefulWidget {
  @override
  _TeamState createState() => _TeamState();
}

class _TeamState extends State<Team> with SingleTickerProviderStateMixin {
  List<String> _tabs = ["队员", "活动"];
  TabController _tabController;
  ScrollController _scrollController;
  int _tabIndex = 0;
  var info;
  var users = [];
  var activites = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
    _scrollController = ScrollController();
    info = Get.arguments;
    Api.getTeamUsers({
      "groupID": info['id'],
    }).then((res) {
      if (res['code'] == 1000) {
        this.users = res['data']['users'];
        setState(() {});
      }
    });
    Api.getTeamActs({
      "groupID": info['id'],
    }).then((res) {
      if (res['code'] == 1000) {
        this.activites = res['data']['activite_list'];
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: extended.NestedScrollView(
      pinnedHeaderSliverHeightBuilder: () {
        return MediaQuery.of(context).padding.top + kToolbarHeight;
      },
      innerScrollPositionKeyBuilder: () {
        if (_tabController.index == 0) {
          return Key('Tab0');
        }
        if (_tabController.index == 1) {
          return Key('Tab1');
        }
      },
      headerSliverBuilder: (context, innerBoxIsScrolled) {
        return <Widget>[
          SliverAppBar(
            title: Text(this.info['groupname']),
            elevation: 0,
            pinned: true,
            expandedHeight: 230.0,
            centerTitle: true,
            brightness: Brightness.dark,
            forceElevated: innerBoxIsScrolled,
            flexibleSpace: FlexibleSpaceBar(
              background: Stack(
                children: [
                  Align(
                    child: Container(
                      width: double.infinity,
                      height: double.infinity,
                      child:
                          Image.asset("images/img/bg04.png", fit: BoxFit.cover),
                    ),
                  ),
                  Align(
                    alignment: Alignment(0, 0.2),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Text(
                          info['introduction'],
                          style: TextStyle(color: Colors.white),
                        ),
                        Container(
                          height: 60,
                          width: 60,
                          child: CircleAvatar(
                            backgroundImage: NetworkImage(
                                HostUtil.getHost() + info['favicon']),
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
            bottom: PreferredSize(
              child: TabBar(
                  labelColor: Colors.white,
                  controller: _tabController,
                  indicatorColor: Color.fromARGB(255, 245, 245, 245),
                  onTap: (index) {
                    setState(() {
                      _tabIndex = index;
                    });
                  },
                  tabs: <Widget>[
                    new Tab(
                      text: '队员',
                    ),
                    new Tab(
                      text: '活动',
                    )
                  ]),
              preferredSize: Size.fromHeight(40),
            ),
          ),
        ];
      },
      body: TabBarView(
        controller: _tabController,
        children: <Widget>[
          extended.NestedScrollViewInnerScrollPositionKeyWidget(
            Key('Tab0'),
            TeamUsers(
              users: users,
            ),
          ),
          extended.NestedScrollViewInnerScrollPositionKeyWidget(
              Key('Tab1'), TeamAct(activites: activites)),
        ],
      ),
    ));
  }
}
